package cn.wolfcode.wms.domain;

import cn.wolfcode.wms.util.StringUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FilenameUtils;

import java.math.BigDecimal;
import java.util.HashMap;

@Setter@Getter
public class Product extends  BaseDomain{

    private String name;

    private String sn;

    private BigDecimal costPrice;

    private BigDecimal salePrice;

    private String imagePath;
    //商品简介
    private String intro;

    private Brand brand;

    public String gteImagePath(){
        return StringUtil.empty2Null(imagePath);
    }
    public String getsmallImagePath() {
        String ext = FilenameUtils.getExtension(imagePath);
        String path = imagePath.substring(0, imagePath.lastIndexOf("."));
        return path + "_small." + ext;
    }

    //自定义属性转成JSon
    public String getJson() throws JsonProcessingException {
        ObjectMapper JSON = new ObjectMapper();
        HashMap<String, Object> map = new HashMap<>();
        map.put("pid",id);//id
        map.put("name",name);//name
        map.put("costPrice",costPrice);//成本价格
        map.put("salePrice",salePrice);//零售价格
        map.put("brandId",brand.id);//品牌ID
        map.put("brandName",brand.getName());//品牌name

        String result = JSON.writeValueAsString(map);
        return result;
    }










}